package com.hoyue.exam.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hoyue.classroom.bean.Course;
import com.hoyue.exam.bean.Question;
import com.hoyue.classroom.service.CourseService;
import com.hoyue.exam.service.QuestionService;

import com.hoyue.exam.vo.QuestionConditionVo;
import com.hoyue.exam.vo.base.PageResultVo;
import com.hoyue.exam.vo.base.ResponseVo;
import com.hoyue.common.utils.CoreConst;
import com.hoyue.common.utils.PageUtil;
import com.hoyue.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author SiMan
 * @date 2019/8/28 0:28
 */

@Controller
@RequestMapping("question")
public class QuestionController {

	@Autowired
	private QuestionService questionService;

	@Autowired
	private CourseService courseService;

	@PostMapping("/list")
	@ResponseBody
	public PageResultVo loadQuestion(QuestionConditionVo questionConditionVo, Integer limit, Integer offset) {
		PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
		List<Question> questionList = questionService.findByCondition(questionConditionVo);
		PageInfo<Question> pages = new PageInfo<>(questionList);

		return ResultUtil.table(questionList, pages.getTotal(),pages);
	}

	//添加题目
	@GetMapping("/add")
	public String addQuestion(Model model) {
		Course course = new Course();
		course.setStatus(CoreConst.STATUS_INVALID);
		List<Course> courses = courseService.selectCourses(course);
		for (Course course1 : courses) {
			System.out.println(course1.getName());
		}
		model.addAttribute("courses", JSON.toJSONString(courses));
		
		return "question/add";
	}

	@PostMapping("/add")
	@ResponseBody
	public ResponseVo add(Question question) {
		try {
			question.setStatus(CoreConst.STATUS_INVALID.toString());
			Question questions = questionService.insertQuestion(question);
			return ResultUtil.success("新增题目成功！");
		} catch (Exception e) {
			return ResultUtil.error("新增题目失败！");
		}
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public ResponseVo delete(Integer id) {
		int i = questionService.deleteBatch(new Integer[]{id});
		if(i > 0) {
			return ResultUtil.success("删除题目成功");
		}else {
			return ResultUtil.error("删除题目失败");
		}
	}
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@PostMapping("/batch/delete")
	@ResponseBody
	public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[]ids) {
		int i = questionService.deleteBatch(ids);
		if(i > 0) {
			return ResultUtil.success("批量删除题目成功");
		}else {
			return ResultUtil.error("批量删除题目失败");
		}
	}

	@GetMapping("/edit")
	public String edit(Model model, Integer id) {
		Question question = questionService.selectById(id);
		model.addAttribute("question", question);
		Course course = new Course();
		course.setStatus(CoreConst.STATUS_INVALID);
		List<Course> courses = courseService.selectCourses(course);
		model.addAttribute("courses",JSON.toJSONString(courses));

		return "question/detail";
	}

	@PostMapping("/edit")
	@ResponseBody
	public ResponseVo edit(Question question) {
		question.setStatus(CoreConst.STATUS_INVALID.toString());
		questionService.updateNotNull(question);
		return ResultUtil.success("编辑题目成功");
	}
}
